<?

class FaceLib{
	static $facebook;
	
	function getProfileImageUrl($id){
		$headers = get_headers("https://graph.facebook.com/$id/picture",1);
		// just a precaution, check whether the header isset...
		if(isset($headers['Location'])) {
			$url = $headers['Location']; // string
		} else {
			$url = false; // nothing there? .. weird, but okay!
		}
		return $url;
	}
	
	//Lay Facebook name voi link
	static function getProfileName($fbme) {
		return '<a href="'.FB_BASE_URL.'profile.php?id='.$fbme['id'].'" target="_blank">'.$fbme['name'].'</a>';
	}
	
	//Lay Facebook image profile voi link
	static function getProfileImage($fbme, $type = '') {
		$src = 'http://graph.facebook.com/'.$fbme['id'].'/picture';
		if($type != '') {
			$src .= '?type='.$type;
		}
		return '<a href="'.FB_BASE_URL.'profile.php?id='.$fbme['id'].'" target="_blank"><img src="'.$src.'"/></a>';
	}
	
	//Lay Noi dung niem tin cua 1 post
	static function getContentBelive($post) {
		if($post['owner']) {
			return '<a href="http://facebook.com/profile.php?id='.$post['fbid'].'" target="_blank">'. $post['fbname'] . '</a> ' . $post['content'];
		}
		else {
			return $post['content'];
		}
	}
	
	//Lay danh sach ban be
	static function getFriends() {
		global $facebook;
		//prt($data);

		try {
			# stream.publish
			$api_call = array(
				'method' 		=> 	'friends.get'
			);
			return $facebook->api($api_call);
			
		} catch (FacebookApiException $e) {
			error_log($e);
		}
	}
	
	//Post du lieu tin len wall facebook, return facebook post id
	static function postContentToWall($data) {
		global $facebook;
		//prt($data);
		//$data = array('message', 'src', 'link_image', 'title', 'caption', 'description', 'text', 'link', properties => text, href);
		$media[] 		= array('type' => 'image', 'src' => $data['src'], 'href' => $data['link_image']);
		$attachments 	= array('name'=>$data['title'], 'href'=>$data['link_image'], 'media'=>$media, 'caption'=>$data['caption'], 'description'=>$data['description'], 'properties' => $data['properties']);
		$action_links[] = array('text'=>$data['text'], 'href'=>$data['link']);
	
		try {
			# stream.publish
			$api_call = array(
				'method' 		=> 	'stream.publish',
				'message'		=>	$data['message'],
				'attachment'	=>	$attachments,
				'action_links'	=>	$action_links
			);
			return $facebook->api($api_call);
			
		} catch (FacebookApiException $e) {
			error_log($e);
		}
	}
	
	//Post link len wall facebook
	static function postLinkToWall($fbme, $data) {
		global $facebook;
		if($fbme) {
			try {
				# stream.publish
				$api_call = array(
					'method' 		=> 	'links.post',
					'url'			=>	$data['link_click'],
					'comment'		=>	$data['content']
				);
				return $facebook->api($api_call);
				
			} catch (FacebookApiException $e) {
				error_log($e);
			}
		}
		else echo "Chưa kết nối Facebook";
	}
	
	static function postActivityToDashboard($fbme, $data) {
		global $facebook;
		//$data = array(message, text, href);
		$action_link 		= 	array('text' => $data['text'], 'href' => $data['href']);
		$activity = array('message' => '{*actor*} '.$data['text'], 'action_link' => $action_link);
		
		if($fbme) {
			try {
				# stream.publish
				$api_call = array(
					'method' 		=> 'dashboard.publishActivity',
					'activity'		=> $activity
				);
				return $facebook->api($api_call);
				
			} catch (FacebookApiException $e) {
				error_log($e);
				prt($e);
			}
		}
		else echo "Chưa kết nối Facebook";
	}
	
	static function postNewsToDashboard($fbme, $data) {
		global $facebook;
		//$data = array('message' => "@:1311054854 đang đi chơi", 'text' => 'Muachung', 'href' => 'http://muachung.vn', 'image' => 'http://profile.ak.fbcdn.net/hprofile-ak-snc4/195658_100000191705512_4642617_q.jpg');
		$news[] = array(
			'message' => $data['message'],
			'action_link' => array('text' => $data['text'], 'href' => $data['href'])
		);

		if($fbme) {
			try {
				# stream.publish
				$api_call = array(
					'method' 		=> 'dashboard.addNews',
					'news'			=> $news,
					'image'			=> $data['image']
				);
				return $facebook->api($api_call);
				
			} catch (FacebookApiException $e) {
				error_log($e);
				prt($e);
			}
		}
		else echo "Chưa kết nối Facebook";
	}
}
?>